package Calendar.toolBar;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import Calendar.table.DateTableModel;
import Calendar.table.DateTablePanel;
import Calendar.table.util.ChineseCalendar;
import Calendar.table.util.DayProvider;

public class ToolBarPanel extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	private int y = 0;
	private int m = 0;
	
	private static final String TODAY 	= "今天" , 
								   YEAR		= "年",
								   MONTH 	= "月",
								   PREVIOUS = "<",
								   NEXT 	= ">";
	
	private JButton todayBtn = new JButton(TODAY);
	private JButton preMonthBtn = new JButton(PREVIOUS);
	private JButton nextMonthBtn = new JButton(NEXT);
	private JComboBox yearComboBox = new JComboBox();
	private JComboBox monthComboBox = new JComboBox();
	
	private JLabel yearLabel = new JLabel(YEAR);
	private JLabel monthLabel = new JLabel(MONTH);
	private JLabel lunarYearLable = new JLabel();
//	private JLabel lunarMonthLable = new JLabel();
	
	private DateTablePanel dateTablePanel;
	private DayProvider dayProvider;
	private ChineseCalendar today;
	boolean flag = true;
	
	public ToolBarPanel(DateTablePanel dateTablePanel) {
		this.dateTablePanel = dateTablePanel;
		dayProvider = new DayProvider();
		today = dayProvider.getToday();
		y = today.get(Calendar.YEAR);
		m = today.get(Calendar.MONTH);
		//init data
		for (int i = 0; i < 200; i++) {
			yearComboBox.addItem(String.valueOf(i + 1901));
			if (i + 1901 == today.get(Calendar.YEAR)) {
				yearComboBox.setSelectedIndex(i);
			}
		}
		
		for (int i = 1; i <= 12; i++) {
			monthComboBox.addItem(String.valueOf(i));
			if (i == (today.get(Calendar.MONTH) + 1)) {
				monthComboBox.setSelectedIndex(i - 1);
			}
		}
		lunarYearLable.setText(today.getChinese(ChineseCalendar.CHINESE_YEAR));
		lunarYearLable.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
		
//		lunarMonthLable.setText(today.getChinese(ChineseCalendar.CHINESE_MONTH));
//		lunarMonthLable.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
		
		//add event
		todayBtn.addActionListener(this);
		preMonthBtn.addActionListener(this);
		nextMonthBtn.addActionListener(this);
		yearComboBox.addActionListener(this);
		monthComboBox.addActionListener(this);
		
		//set layout
		setLayout(new MigLayout());
		setSize(450, 40);
		
		//
		add(todayBtn, "cell 0 0 2 1");
		add(preMonthBtn, "cell 2 0 1 1");
		add(yearComboBox, "cell 3 0 3 1");
		add(yearLabel, "cell 6 0 1 1");
		add(monthComboBox, "cell 7 0 2 1");
		add(monthLabel, "cell 9 0 1 1");
		add(nextMonthBtn, "cell 10 0 1 1");
		add(lunarYearLable, "cell 11 0 3 1");
//		add(lunarMonthLable, "cell 14 0 1 1");
	}
	
	private void update(){
		ChineseCalendar currentDay = dayProvider.getChineseCalendar(y, m);
		lunarYearLable.setText(currentDay.getChinese(ChineseCalendar.CHINESE_YEAR));
//		lunarMonthLable.setText(currentDay.getChinese(ChineseCalendar.CHINESE_MONTH));
		
		y = currentDay.get(Calendar.YEAR);
		m = currentDay.get(Calendar.MONTH);
		flag = false;
		yearComboBox.setSelectedItem(String.valueOf(y));
		monthComboBox.setSelectedItem(String.valueOf(m + 1));
		flag = true;
		
		dateTablePanel.update(new DateTableModel(dayProvider.getMonthData(y, m)));
		dateTablePanel.updateUI();
		
		this.updateUI();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == todayBtn) {
			y = today.get(Calendar.YEAR);
			m = today.get(Calendar.MONTH);
			update();
		}
		if (e.getSource() == preMonthBtn) {
			m = m - 1;
			update();
		}
		if (e.getSource() == nextMonthBtn) {
			m = m + 1;
			update();
		}
		if (e.getSource() == yearComboBox && flag) {
			y = Integer.parseInt((String) yearComboBox.getSelectedItem());
			update();
		}
		if (e.getSource() == monthComboBox && flag) {
			m = Integer.parseInt((String) monthComboBox.getSelectedItem()) - 1;
			update();
		}
	}
}
